package com.newlink.building.common_base.widget

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.core.content.ContextCompat
import com.newlink.building.common_base.R

/**
 * @Author: Jake
 * @Date: 2024-10-31
 * @Description:
 */
class EmptyContentView @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : LinearLayout(context, attrs, defStyleAttr) {
    private var imageView: ImageView
    private var textView: TextView

    init {
        LayoutInflater.from(context).inflate(R.layout.view_empty_layout, this, true)

        orientation = VERTICAL

        imageView = findViewById(R.id.customImageView)
        textView = findViewById(R.id.customTextView)

        attrs?.let {
            val typedArray = context.obtainStyledAttributes(it, R.styleable.ImageTextView, 0, 0)

            val imageResId = typedArray.getResourceId(R.styleable.ImageTextView_imageSrc, 0)
            if (imageResId != 0) {
                imageView.setImageDrawable(ContextCompat.getDrawable(context, imageResId))
            }

            val text = typedArray.getString(R.styleable.ImageTextView_text)
            textView.text = text ?: ""

            typedArray.recycle()
        }
    }

    fun setImageResource(resId: Int) {
        imageView.setImageResource(resId)
    }

    fun setText(text: String) {
        textView.text = text
    }
}